package aygx.integration.schedule.impl;

import aygx.integration.config.CustomTaskScheduler;
import aygx.integration.schedule.ScheduleService;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 暗影孤星
 * @date 2022/5/20 17:46
 * @description
 */
@Slf4j
@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private CustomTaskScheduler customTaskScheduler;

    Integer count = 0;

//    2022-05-23 14:31:28.012  INFO 15784 --- [   scheduling-1] : scheduleTest2,2022-05-23T14:31:28.012
//    2022-05-23 14:31:31.014  INFO 15784 --- [   scheduling-1] : scheduleTest1,2022-05-23T14:31:31.014
//    2022-05-23 14:31:34.016  INFO 15784 --- [   scheduling-1] : scheduleTest2,2022-05-23T14:31:34.016
//    2022-05-23 14:31:37.016  INFO 15784 --- [   scheduling-1] : scheduleTest1,2022-05-23T14:31:37.016
//    2022-05-23 14:31:40.016  INFO 15784 --- [   scheduling-1] : scheduleTest2,2022-05-23T14:31:40.016
//    2022-05-23 14:31:43.016  INFO 15784 --- [   scheduling-1] : scheduleTest1,2022-05-23T14:31:43.016
//    2022-05-23 14:31:46.017  INFO 15784 --- [   scheduling-1] : scheduleTest2,2022-05-23T14:31:46.017
//    2022-05-23 14:31:49.017  INFO 15784 --- [   scheduling-1] : scheduleTest1,2022-05-23T14:31:49.017
//    2022-05-23 14:31:52.017  INFO 15784 --- [   scheduling-1] : scheduleTest2,2022-05-23T14:31:52.017
//    2022-05-23 14:31:55.017  INFO 15784 --- [   scheduling-1] : scheduleTest1,2022-05-23T14:31:55.017

    @Scheduled(cron = "* * * * * ?")
    public void scheduleTest1() {
        ThreadUtil.safeSleep(3000);
        log.info("scheduleTest1,{}", LocalDateTime.now());
    }

    @Scheduled(cron = "* * * * * ?")
    public void scheduleTest2() {
        count++;
        ThreadUtil.safeSleep(3000);
        log.info("scheduleTest2,{}", LocalDateTime.now());

//        2022-05-27 15:43:01.001  INFO 7192 --- [ Scheduled-ZXX6]: scheduleTest2,2022-05-27T15:43:01.001
//        2022-05-27 15:43:01.001  INFO 7192 --- [ Scheduled-ZXX6]: 任务已执行指定次数，现在自动停止
//        2022-05-27 15:43:01.001  INFO 7192 --- [ Scheduled-ZXX6]: addTriggerTask,2022-05-27T15:43:01.001
//        2022-05-27 15:43:02.001  INFO 7192 --- [Scheduled-ZXX10]: addTriggerTask,2022-05-27T15:43:02.001
        if (count >= 3) {
            log.info("任务已执行指定次数，现在自动停止");
            customTaskScheduler.getScheduledTasks().get(this).cancel(true);
        }
    }
}
